<?php

namespace App\Http\Controllers;

use App\Cat;
use App\Data;
use App\Article;
use App\Http\Resources\ArticleCollection;
use App\Http\Resources\CatCollection;
use App\Http\Resources\DataCollection;
use App\Http\Resources\MapCollection;
use App\Map;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Intervention\Image\Facades\Image;
use Intervention\Image\Imagick\Font;

class DataController extends Controller
{
    //模型详情
    public function index($id, $method)
    {
        $info = Data::where(['is_del' => 0, 'status' => 1])->findOrfail($id);
        $info->viewnum += 1;
        $info->save();
        $data = new \App\Http\Resources\Data($info);
        return $data;
    }

    //案例详情
    public function article($id, $method)
    {
        $info = Article::where('is_del', 0)->findOrfail($id);
        $info->viewnum += 1;
        $info->save();
        return new \App\Http\Resources\Article($info);
    }

    //返回上传的分类与风格
    public function create($type)
    {
        $cats = Cat::where('type', $type)->get();
        $cats = new CatCollection($cats);
        $data['type'] = $type;
        $data['cats'] = collect($cats)->groupBy('father_id');
        //设计风格
        if ($type == 1 || $type == 2 || $type == 7) {
            $style = Cat::where('type', 8)->get();
            $data['style'] = new CatCollection($style);
        }
        $data['status'] = 1;
        $data['message'] = 'success';
        return response()->json($data);
    }


    //3d~su模型上传
    public function store(Request $request)
    {
        $data = $request->validate([
            'type' => 'required',
            'title' => 'required',
            'file' => 'required',
            'filesize' => 'required',
            'image' => 'required',
            'appversion' => '',
            'renderertype' => '',
            'cat_id1' => 'required',
            'cat_id2' => '',
            'style' => '',
            'is_light' => '',
        ]);
        $data['user_id'] = request()->user()->id;
        $data['images'] = $data['image'];
        for ($i = 2; $i <= 5; $i++) {
            if (request('image' . $i)) {
                $data['images'] .= ',' . request('image' . $i);
            }
        }
        $res = Data::create($data);
        if ($res) {
            $this->makethumb($res);
            return response()->json(['message' => '上传成功', 'status' => 1]);
        } else {
            return response()->json(['message' => '上传失败', 'status' => 0]);
        }
    }

    //分类列表数据
    public function show($type, $catid1, $catid2, $style, $id, $keys, $sort, $vip, $method)
    {
        !empty($catid1) ? $where['cat_id1'] = $catid1 : '';
        !empty($keys) ? $whereId[] = ['id', 'like', '%' . $keys . '%'] : $whereId['type'] = $type;
        !empty($keys) ? $whereTitle[] = ['title', 'like', '%' . $keys . '%'] : $whereTitle['type'] = $type;
        $where['status'] = 1;
        !empty($sort) ? $order = $sort : $order = 'created_at';
        if ($type < 6) {
            $where['is_del'] = 0;
            $vip == 1 ? $where['is_vip'] = $vip : '';
            $where['type'] = $type;
            $orwhere['type'] = $type;
            !empty($catid2) ? $where['cat_id2'] = $catid2 : '';
            !empty($style) ? $where['style'] = $style : '';
            $items = Data::where($where)->where(function ($query) use ($whereId, $whereTitle) {
                $query->where($whereId)
                    ->orWhere(function ($query) use ($whereTitle) {
                        $query->where($whereTitle);
                    });
            })->orderBy($order, 'desc')->paginate(30);
            $data = new DataCollection($items);
        } elseif ($type == 6) {
            !empty($keys) ? $where[] = ['title', 'like', '%' . $keys . '%'] : '';
            $items = Map::where($where)->orderBy($order, 'desc')->paginate(40);
            $data = new MapCollection($items);
        } else {
            !empty($keys) ? $where[] = ['title', 'like', '%' . $keys . '%'] : '';
            $where['is_del'] = 0;
            !empty($style) ? $where['style'] = $style : '';
            !empty($catid2) ? $where['cat_id2'] = $catid2 : '';
            $items = Article::where($where)->orderBy($order, 'desc')->paginate(30);
            $data = new ArticleCollection($items);
        }
        return $data;
    }

    //用户数据列表
    public function usershow($user_id, $type, $method)
    {
        $where['status'] = 1;
        $where['is_del'] = 0;
        $userinfo = User::find($user_id);
        //等于8时代表请求上传时间轴
        if ($type == 8) {
            $where['user_id'] = $user_id;
            $items = new DataCollection(Data::where($where)
                ->select('id', 'title', 'type', 'image', DB::raw('DATE_FORMAT(created_at,"%Y-%m") as month'))
                ->orderBy('created_at', 'desc')->get());
            return collect($items)->groupBy('month');
        }
        $where['type'] = $type;
        $data = new DataCollection($userinfo->datas()->where($where)->orderBy('created_at', 'desc')->paginate(16));
        return $data;
    }
    //上传制作缩略图
    public function makethumb($res)
    {
        $img_url = public_path('/admin/images/watermark.png');
        $watermark = Image::make($img_url);
        $watermark->text('ID:' . $res->id, 0, 65, function (Font $font) {
            $font->file(public_path('/admin/images/font.otf'));
            $font->size(28);
            $font->valign('bottom');
            $font->color('#EEE');
        });
        $watermark->opacity(60);
        foreach ($res->images as $img) {
            $img = public_path('storage/' . $img);
            $img = Image::make($img)->widen(1200)->insert($watermark, 'bottom-right', 50, 30);
            $save_path = 'image/' . @date("Ymd", time()) . '/' . md5(rand(100000000, 999999999)) . '_thumb' . '.' . $img->extension;
            $real_save_path = public_path('storage/') . $save_path;
            $img->save($real_save_path);
            $image_thumbs[] = $save_path;
        }
        $res->update(['images_thumb' => implode(',', $image_thumbs)]);
    }
    //相关推荐
    public function nearby($type,$cat_id2,$id)
    {
        $where['is_del'] = 0;
        if($type !=4 ){
            $where['cat_id2'] = $cat_id2;
        }else{
            $where['cat_id1'] = $cat_id2;
        }
        if($type<7){
            return  new DataCollection(Data::where($where)->inRandomOrder()->where('id', '!=',$id)->limit(4)->get());
        }
        return  new ArticleCollection(Article::where($where)->inRandomOrder()->where('id', '!=',$id)->limit(4)->get());
    }
}
